import DictionConstUtil from '@/utils/diction-const-util';
export default class control {
    /**
     * 校验所填参数是否正确
     */
    static async verifyItemDetailParam(entryDetail: any, isO32?: boolean): Promise<any> {
        // 条目基础信息 */
        const entryControl = entryDetail.entryControl;
        // 基础信息-名称
        if (!entryControl.entryName) {
            return '风险说明不能为空';
        } else {
            if (entryControl.entryName.length > 300) {
                return '风险说明太长，不能超过300个字符';
            }
        }
        if (!entryControl.useO32No && isO32) {
            return '对应O32风控序号不能为空';
        }
        // 基础信息-风控级别
        if (entryControl.level === null) {
            return '条目优先级不能为空';
        }
        // 基础信息-控制类型
        if (!entryControl.accountControlType) {
            return '控制类型不能为空';
        }
        // 基础信息-账户控制值
        const accountControlValue = entryControl.accountControlValue;
        const accControlName: any = {
            '0': '控制账户',
            '1': '账户组',
            '2': '资产单元',
            '3': '资产单元组',
            '4': '投资组合',
            '5': '投资组合组',
        };
        if (!accountControlValue || accountControlValue.length === 0) {
            return accControlName[entryControl.originalAccountId] + '不能为空';
        }

        // 基础信息-账户控制值
        const oldRiskCalTypes = entryControl.riskCalTypes;
        if (!oldRiskCalTypes || oldRiskCalTypes.length === 0) {
            return '作用范围至少选一个';
        }
        if (entryControl.beginDate && entryControl.endDate) {
            if (Number(entryControl.beginDate) > Number(entryControl.endDate)) {
                return '失效日期要大于生效日期';
            }
        }
        // 基础信息--阈值
        if (!['3', '5', '11'].includes(entryControl.entryType)) {
            if (
                (entryControl.thresholdType1 && !entryControl.thresholdValue1 && entryControl.thresholdValue1 !== 0) ||
                (!entryControl.thresholdType1 && (entryControl.thresholdValue1 === 0 || entryControl.thresholdValue1))
            ) {
                return '阀值1设置不完整';
            }
            if (entryControl.thresholdType2) {
                if (
                    (entryControl.thresholdType2 && !entryControl.thresholdValue2 && entryControl.thresholdValue2 !== 0) ||
                    (!entryControl.thresholdType2 && (entryControl.thresholdValue2 === 0 || entryControl.thresholdValue2))
                ) {
                    return '阀值2设置不完整';
                }
            }
            if (entryControl.thresholdType3) {
                if (
                    (entryControl.thresholdType3 && !entryControl.thresholdValue3 && entryControl.thresholdValue3 !== 0) ||
                    (!entryControl.thresholdType3 && (entryControl.thresholdValue3 === 0 || entryControl.thresholdValue3))
                ) {
                    return '阀值3设置不完整';
                }
            }

            if (entryControl.thresholdType3 && !entryControl.thresholdType2) {
                return '阀值2不可为空';
            }
            // 02大于 13小于
            if (['0', '2'].includes(entryControl.compareType)) {
                // eslint-disable-next-line prefer-const
                let list = [];
                if (entryControl.thresholdValue1 || entryControl.thresholdValue1 === 0) {
                    list.push(entryControl.thresholdValue1);
                }
                if (entryControl.thresholdValue2 || entryControl.thresholdValue2 === 0) {
                    list.push(entryControl.thresholdValue2);
                }
                if (entryControl.thresholdValue3 || entryControl.thresholdValue3 === 0) {
                    list.push(entryControl.thresholdValue3);
                }
                let firstValue = null;
                if (list.length > 0) {
                    firstValue = list[0];
                }
                for (let i = 1; i < list.length; i++) {
                    if (parseFloat(list[i]) <= parseFloat(firstValue)) {
                        console.log(parseFloat(list[i]));
                        console.log(parseFloat(firstValue));

                        return '比较方向 大于或大于等于 时 控制值需逐渐递增';
                    } else {
                        firstValue = list[i];
                    }
                }
            } else if (['1', '3'].includes(entryControl.compareType)) {
                // eslint-disable-next-line prefer-const
                let list = [];
                if (entryControl.thresholdValue1 || entryControl.thresholdValue1 === 0) {
                    list.push(entryControl.thresholdValue1);
                }
                if (entryControl.thresholdValue2 || entryControl.thresholdValue2 === 0) {
                    list.push(entryControl.thresholdValue2);
                }
                if (entryControl.thresholdValue3 || entryControl.thresholdValue3 === 0) {
                    list.push(entryControl.thresholdValue3);
                }
                let firstValue = null;
                if (list.length > 0) {
                    firstValue = list[0];
                }
                for (let i = 1; i < list.length; i++) {
                    if (parseFloat(list[i]) >= parseFloat(firstValue)) {
                        return '比较方向 小于或小于等于 时 控制值需逐渐递减';
                    } else {
                        firstValue = list[i];
                    }
                }
            }
            // o32规定：值不可大于999999999.0000！保留四位小数
            // NUMBER(20,6)  值不可大于99999999999999.999999;
            console.log(entryControl.thresholdValue1, 'entryControl.thresholdValue1');
            const maxThresTypeValue = 99999999999999.999999;
            const showMaxThresTypeValue = '99,999,999,999,999.999999';
            if (entryControl.thresholdValue1 && entryControl.thresholdValue1 > maxThresTypeValue) {
                return '阈值1不可大于' + showMaxThresTypeValue;
            } else if (entryControl.thresholdValue2 && entryControl.thresholdValue2 > maxThresTypeValue) {
                return '阈值2不可大于' + showMaxThresTypeValue;
            } else if (entryControl.thresholdValue3 && entryControl.thresholdValue3 > maxThresTypeValue) {
                return '阈值3不可大于' + showMaxThresTypeValue;
            }
        }

        // 条目分支信息1 */
        const riskEntryBranchs = entryDetail.riskEntryBranchs;
        if ((!riskEntryBranchs || riskEntryBranchs.length === 0) && !entryControl.useO32No) {
            return '条目分支不能为空';
        } else {
            for (let i = 0; i < riskEntryBranchs.length; i++) {
                // eslint-disable-next-line prefer-const
                let entryBanchS = riskEntryBranchs[i].entryBranch;
                if (entryBanchS.branchType === '1') {
                    // eslint-disable-next-line prefer-const
                    let isFund = riskEntryBranchs.find(item => String(item.entryBranch.parentBranchNo) === String(entryBanchS.branchNo));
                    if (!isFund) {
                        return '条目分支解析失败，存在空的分支';
                    }
                }
                // eslint-disable-next-line prefer-const
                let entryBranchConfigs = riskEntryBranchs[i].entryBranchConfigs;

                if (entryBanchS.factorCode == 'YH513') {
                    let flag = false;
                    for (let index = 0; index < entryBranchConfigs.length; index++) {
                        const item = entryBranchConfigs[index];
                        if (item.boundCode == 'YH656') {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        return '“' + entryBanchS.branchName + '”' + '未配置相关范围条件';
                    }
                }

                for (let j = 0; j < entryBranchConfigs.length; j++) {
                    if (entryBranchConfigs[j].boundGroupValid === true) {
                        // eslint-disable-next-line prefer-const
                        let isFund = entryBranchConfigs.find(item => String(item.parentId) === String(entryBranchConfigs[j].configId));
                        if (!isFund) {
                            console.log(entryBranchConfigs[j], 'entryBranchConfigs[j]');
                            console.log(riskEntryBranchs[i], '!@!@');

                            // 所在分支
                            const branch = riskEntryBranchs[i].entryBranch.label;
                            // 所在范围属性
                            let groupType = '';
                            if (entryBranchConfigs[j].groupType !== '0') {
                                groupType = entryBranchConfigs[j].groupType;
                            } else {
                                groupType = entryBranchConfigs.find(item => String(item.configId) === String(entryBranchConfigs[j].parentId))
                                    ?.groupType;
                            }
                            return (
                                '条目分支范围解析失败，"' +
                                branch +
                                '" -> "' +
                                DictionConstUtil.BoundType[groupType] +
                                '"存在空的分支范围，请前往检查'
                            );
                        }
                    }
                }

                for (let f = 0; f < entryBranchConfigs.length; f++) {
                    console.log(entryBranchConfigs, 'entryBranchConfigs----------');

                    if (entryBranchConfigs[f].showBoundExtend === true && !entryBranchConfigs[f].boundExtend) {
                        if (entryBranchConfigs[f].resolveKey) {
                            return '范围配置' + entryBranchConfigs[f].resolveKey + '类型动态参数设置不能为空';
                        }
                        return '范围配置5类型动态参数设置不能为空';
                    }
                }
            }
        }

        // 分支动态参数配置
        // eslint-disable-next-line prefer-const
        let tnFactorPojo = {};
        // eslint-disable-next-line func-call-spacing
        const DictionConst = await DictionConstUtil.FNCalculateFactor();
        (DictionConst || []).forEach(item => {
            tnFactorPojo[item.entryCalculateFactorCode] = item;
        });
        for (const vc of riskEntryBranchs) {
            if (vc.entryBranch.factorCode == 'HT001') {
                // 华泰计算因子HT001不做这一步检测
                if (vc.entryBranch.entryCalculateFactorDynamicParam == '') {
                    return '分支 ' + vc.entryBranch.branchName + ' 动态参数未配置完整'
                }
                continue;
            }
            if (String(vc.entryBranch.branchType) === '1') {
                continue;
            }
            // eslint-disable-next-line prefer-const
            let factorPojo = tnFactorPojo[vc.entryBranch.factorCode];
            if (factorPojo.existDynamicCfg && factorPojo.dynamicCfg && factorPojo.dynamicCfg.length > 0) {
                // eslint-disable-next-line prefer-const
                let paramValueCac = {};
                // eslint-disable-next-line no-unneeded-ternary
                const pvCac = (vc.entryBranch.entryCalculateFactorDynamicParam || '').split(',').filter((ic: any) => (ic ? true : false));
                for (const value of pvCac) {
                    if (value && value.includes(':')) {
                        paramValueCac[value.substr(0, value.indexOf(':'))] = value.substr(value.indexOf(':') + 1);
                    }
                }
                
                for (const v of factorPojo.dynamicCfg) {
                    if (!paramValueCac[v]) {
                        return '分支 ' + vc.entryBranch.branchName + ' 动态参数未配置完整';
                    }
                }
            }
        }

        // 条目主表可投余量
        if (entryControl.usableSubstitute && entryControl.usableSubstitute.trim() && !['3', '5'].includes(entryControl.entryType)) {
            // eslint-disable-next-line prefer-const
            let selectDataSource = [];
            selectDataSource.push({ id: 'X', name: '控制值' });
            if (entryControl.thresholdType1) {
                selectDataSource.push({ id: 'A', name: '控制值1' });
            }
            if (entryControl.thresholdType2) {
                selectDataSource.push({ id: 'B', name: '控制值2' });
            }
            if (entryControl.thresholdType3) {
                selectDataSource.push({ id: 'C', name: '控制值3' });
            }
            if (riskEntryBranchs && riskEntryBranchs.length > 0) {
                // eslint-disable-next-line prefer-const
                for (let value of riskEntryBranchs) {
                    if (value.entryBranch.branchType === '0') {
                        selectDataSource.push({ id: String(value.entryBranch.branchNo), name: value.entryBranch.branchName });
                    }
                }
            }
            selectDataSource.push({ id: 'D', name: '计算结果' });
            // eslint-disable-next-line prefer-const
            let usableSubstitute = entryControl.usableSubstitute;
            // .replace(/Math\.abs\(/, 'ABS(')
            // .replace(/Math\.min\(/, 'MIN(')
            // .replace(/Math\.max\(/, 'MAX(') || '';
            // eslint-disable-next-line prefer-const
            // const branchType = entryDetail.riskEntryBranchs.map(element => element.entryBranch.countType);
            // if (branchType.includes('4')) {
            // 移除绝对值
            usableSubstitute = usableSubstitute.replace(/ABS\(/, '(') || '';
            // }
            // if (branchType.includes('5')) {
            // 取负
            usableSubstitute = usableSubstitute.replace(/MIN\(/, '(') || '';
            // }
            // if (branchType.includes('6')) {
            usableSubstitute = usableSubstitute.replace(/MAX\(/, '(') || '';
            // }
            const regex3 = /\$\{(.+?)\}/g;
            // eslint-disable-next-line prefer-const
            let fv = usableSubstitute.match(regex3) || [];
            // eslint-disable-next-line prefer-const
            let cv = usableSubstitute.replace(regex3, '@@').split('@@') || [];
            let name = '';
            for (let i = 0; i < cv.length; i++) {
                name += cv[i];
                if (fv[i]) {
                    // eslint-disable-next-line prefer-const
                    let sn = selectDataSource.find(item => item.id === fv[i].substr(2, fv[i].length - 3)) || {};
                    if (sn.name) {
                        name += '1';
                    }
                }
            }
            if (name.length === 1 || name.trim().length === 1) {
                return '可投余量配置错误';
            }

            try {
                // eval 并不是仅仅作为解析字符串用。。更多的是执行动态代码上面，例如把字符串作为代码执行
                eval('let result2 = ' + name); // eslint-disable-line
            } catch (e) {
                return '可投余量配置错误';
            }
        }

        // 高级设置-启用时段验证
        // eslint-disable-next-line prefer-const
        let entryParams = entryDetail.entryParams || [];
        // eslint-disable-next-line prefer-const
        let epe = entryParams.find(item => item.paramType === 'a');
        if (epe && epe.paramValue && epe.paramValue.trim()) {
            console.log(epe.paramValue, 'epe.paramValue');

            // eslint-disable-next-line prefer-const
            let epes = epe.paramValue.split('|');
            console.log(epes, 'epes');

            // eslint-disable-next-line prefer-const
            for (let value of epes) {
                if (!value || !value.trim()) {
                    return '时间段格式不正确';
                }
                // eslint-disable-next-line prefer-const
                let time2 = value.split(',') || [];
                if (
                    time2.length !== 2 ||
                    !time2[0] ||
                    !time2[1] ||
                    time2[0].trim().length !== 6 ||
                    time2[1].trim().length !== 6 ||
                    !/^[0-9]*$/.test(time2[0].trim()) ||
                    !/^[0-9]*$/.test(time2[1].trim()) ||
                    Number(time2[0].trim()) > Number(time2[1].trim()) ||
                    Number(time2[0].trim().substr(0, 2)) > 23 ||
                    Number(time2[0].trim().substr(2, 2)) > 59 ||
                    Number(time2[0].trim().substr(4, 2)) > 59 ||
                    Number(time2[1].trim().substr(0, 2)) > 23 ||
                    Number(time2[1].trim().substr(2, 2)) > 59 ||
                    Number(time2[1].trim().substr(4, 2)) > 59
                ) {
                    return '时间段格式不正确';
                }
            }
        }
        // 基金范围 验证
        // eslint-disable-next-line prefer-const
        let fundRanges = entryDetail.fundRanges;
        if (fundRanges && fundRanges.length > 0) {
            for (let j = 0; j < fundRanges.length; j++) {
                if (fundRanges[j].boundGroupValid === true) {
                    // eslint-disable-next-line prefer-const
                    let isFund = fundRanges.find(item => String(item.parentId) === String(fundRanges[j].configId));
                    if (!isFund) {
                        return '基金范围解析失败，存在空的基金范围组';
                    }
                }
            }
        }
        return 'SUCCESS';
    }
}
